package com.qf.dao;

import com.qf.pojo.User;
import com.qf.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.dao
 * @Description:
 * @Date 2022/5/21 15:04
 */
public class UserDaoImpl implements UserDao {
    //创建Dbutil工具  创建这个对象 其实就是java代码 与数据库进行连接
    private QueryRunner queryRunner =  new QueryRunner(DBUtil.getDateSource());
    @Override
    public User findByUserName(String username) {
        String sql ="select * from tb_user where username =?";
        try {
            User user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
                return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addUser(User user) {
        String sql ="INSERT into tb_user VALUES(null,?,?,?,?,?,?,?)";
        try {
            int count = queryRunner.update(sql, user.getUsername(), user.getPassword(), user.getEmail(),
                    user.getGender(), user.getStatus(), user.getRole(), user.getCode());
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  0;
    }


}
